<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>日历</title>
  <style type="text/css">
  * {
    margin: 0px;
    padding: 0px;
  }
  #data {
    width: 280px;
    border: 1px solid #000000;
    margin: 20px auto;
  }
  #data > p {
    display: flex;
  }
  #data > h5 {
    text-align:center;
  }
  #data > p > span {
    padding: 0 10px;
  }
  #prev,#next {
    cursor: pointer;
  }
  #nian {
    flex: 1;
    text-align: center;
  }
  #title {
    overflow: hidden;
    list-style: none;
    background: #ccc;
  }
  #title > li {
    float:left;
    width: 40px;
    height: 26px;
    line-height: 26px;
    text-align: center;
  }
  #date {
    overflow: hidden;
    list-style: none;
  }
  #date > li {
    float: left;
    width: 34px;
    height: 34px;
    margin: 1px 1px;
    border: 2px solid rgba(0,0,0,0);
    line-height: 34px;
    text-align: center;
    cursor: pointer;
  }
  #date > .hover:hover {
    border:2px solid pink;
  }
  .active {
    color:red
  }
  </style>
</head>
<body>
  <div id="data">
    <p>
    <span id="prev">上一月</span>
    <span id="nian">2022</span>
    <span id="next">下一月</span>
    </p>
    <h5 id="yue">五月</h5>
    <ul id="title">
      <li>日</li>
      <li>一</li>
      <li>二</li>
      <li>三</li>
      <li>四</li>
      <li>五</li>
      <li>六</li>
    </ul>
    <ul id="date">

    </ul>
  </div>
  <script>
  let date = new Date(); // 获取时间对象
    add(); //当页面第一次加载时进行渲染
    function add (){
      
      let cYear = date.getFullYear();//获取年份
      let cMonth = date.getMonth();//获取月份
      let cDay = date.getDate();//获取当天日期

      // 每个月的第一天是周几
      let day = new Date(cYear,cMonth,1).getDay();
      // 获取每个月天数
      let tian = new Date(cYear,cMonth+1,-1).getDate()+1;

      let arr=["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"]

      document.getElementById('nian').innerHTML=cYear;
      document.getElementById('yue').innerHTML=arr[cMonth];

      let html='';
      for(var i=0;i<day;i++){
        html += '<li></li>'
      }
      for (var i = 1;i<=tian;i++){
        // 渲染日期上去，添加样式
        if (i==cDay){
          html += '<li class = "active">'+i+'</li>'
        }
        else{
          html += '<li class = "hover">'+i+'</li>'
        }
      }
      document.getElementById('date').innerHTML=html;
    }
    document.getElementById('prev').onclick=function(){
      date.setMonth(date.getMonth()-1);
      add();
    };
    document.getElementById('next').onclick=function(){
      date.setMonth(date.getMonth()+1);
      add();
    }
  </script>
</body>
</html>